<#include "/includes/vars.inc"> 
<@pp.dropOutputFile />
<#assign fileName = "${javaDir}${packageDir}/service/support/AuthenticationService.java">
<#if includeLoginSpring && !pp.outputFileExists(fileName)>
	<@pp.changeOutputFile name="${fileName}" />

package ${project.@rootPackage}.service.support;

import javax.annotation.Resource;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Authentication service class
 * <#if project.@author[0]??>@author ${project.@author}</#if>
 */
@Service
public class AuthenticationService {

	private static final Logger logger = Logger.getLogger("AuthenticationService");

    @Resource(name = "authenticationManager")
    private AuthenticationManager authenticationManager; // specific for Spring Security

    public boolean login(String username, String password) {
        try {
            System.out.println("Auth: " + username + " " + password);
            Authentication auth = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(
                    username, password));
            if (auth.isAuthenticated()) {
                SecurityContextHolder.getContext().setAuthentication(auth);
                return true;
            }
        } catch (AuthenticationException e) {
            System.out.println("Error al autenticar");
            System.out.println(e);
        }
        return false;
    }

    //@RolesAllowed({"ROLE_ADMIN", "ROLE_USER"})
    public void logout() {
        SecurityContextHolder.getContext().setAuthentication(null);
        //currentUser.unauthenticate();
    }

    public String getUsername() {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();

        if (auth != null) {
            Object principal = auth.getPrincipal();
            if (principal instanceof UserDetails) {
                return ((UserDetails) principal).getUsername();
            }
            return (String) principal.toString();
        }
        return null;
    }

    /**
     * Retrieve the current UserDetails bound to the current thread by Spring Security, if any.
     */
    public UserDetails getUserDetails() {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();

        if (auth != null && auth.getPrincipal() instanceof UserDetails) {
            return ((UserDetails) auth.getPrincipal());
        }

        return null;
    }

    public boolean isLoggedIn() {
        //Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        //return auth!=null && auth.isAuthenticated();
        String userName = getUsername();
        return userName != null && !userName.equals("anonymous");
    }
}

</#if>